/**
 * @file can.c
 * @author  xiaowine (xiaowine@sina.cn)
 * @brief
 * @version 01.00
 * @date    2025-04-08
 *
 * @copyright Copyright (c) {2020}  xiaowine
 *
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2025-04-08 <td>1.0     <td>wangh     <td>内容
 * </table>
 * ******************************************************************
 * *                   .::::
 * *                 .::::::::
 * *                ::::::::::
 * *             ..:::::::::::
 * *          '::::::::::::
 * *            .:::::::::
 * *       '::::::::::::::..        女神助攻,流量冲天
 * *            ..::::::::::::.     永不宕机,代码无bug
 * *          ``:::::::::::::::
 * *           ::::``:::::::::'        .:::
 * *          ::::'   ':::::'       .::::::::
 * *        .::::'      ::::     .:::::::'::::
 * *       .:::'       :::::  .:::::::::' ':::::
 * *      .::'        :::::.:::::::::'      ':::::
 * *     .::'         ::::::::::::::'         ``::::
 * * ...:::           ::::::::::::'              ``::
 * *```` ':.          ':::::::::'                  ::::.
 * *                   '.:::::'                    ':'````.
 * ******************************************************************
 */

/* Private includes ----------------------------------------------------------*/

#include "can.h"
#include "tim.h"
#include "string.h"
#include "uart.h"
/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/
#define RX_Filter 0x0

#define RX_MAILBOX_CAN0 (0UL)  // CAN0接收邮箱号
#define TX_MAILBOX_CAN0 (1UL)  // CAN0发送邮箱号
#define RX_MAILBOX_CAN1 (2UL)  // CAN1接收邮箱号
#define TX_MAILBOX_CAN1 (3UL)  // CAN1发送邮箱号
#define RX_MAILBOX_CAN2 (4UL)  // CAN2接收邮箱号
#define TX_MAILBOX_CAN2 (5UL)  // CAN2发送邮箱号
/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/
char IRQ_CAN0_RX = 0;  // CAN0接收中断标志
char IRQ_CAN1_RX = 0;  // CAN1接收中断标志
char IRQ_CAN2_RX = 0;  // CAN1接收中断标志

can_message_t rx_can0_msg;  // CAN0接收消息
can_message_t rx_can1_msg;  // CAN1接收消息
can_message_t rx_can2_msg;  // CAN2接收消息

/* Private function prototypes -----------------------------------------------*/

/* Private user code ---------------------------------------------------------*/

void canCallBackFun(uint32_t instance, can_event_t event, uint32_t Idx, void *driverState) {
    (void)instance;
    (void)Idx;
    flexcan_state_t *state = driverState;

    if (event == CAN_EVENT_RX_COMPLETE) {
        if (state->callbackParam == (void *)1) {
            CAN_Receive(&can_pal1_instance, RX_MAILBOX_CAN0, &rx_can0_msg);
            IRQ_CAN0_RX = 1;
        }
        else if (state->callbackParam == (void *)2) {
            CAN_Receive(&can_pal2_instance, RX_MAILBOX_CAN1, &rx_can1_msg);
            IRQ_CAN1_RX = 1;
        }
        else if (state->callbackParam == (void *)3) {
            CAN_Receive(&can_pal3_instance, RX_MAILBOX_CAN2, &rx_can2_msg);
            IRQ_CAN2_RX = 1;
        }
    }
    else if (event == CAN_EVENT_TX_COMPLETE) {
        // 发送完成
    }
}

bool canInit(void) {

    can_buff_config_t Rx_buffCfg = {
        .enableFD  = false,
        .enableBRS = false,
        .fdPadding = false,
        .idType    = CAN_MSG_ID_STD,
        .isRemote  = false,
    };
    can_buff_config_t Tx_buffCfg = {
        .enableFD  = false,
        .enableBRS = false,
        .fdPadding = false,
        .idType    = CAN_MSG_ID_STD,
        .isRemote  = false,
    };
    CAN_Init(&can_pal1_instance, &can_pal1_Config0);
    CAN_ConfigRxBuff(&can_pal1_instance, RX_MAILBOX_CAN0, &Rx_buffCfg, RX_Filter);
    CAN_ConfigTxBuff(&can_pal1_instance, TX_MAILBOX_CAN0, &Tx_buffCfg);
    CAN_SetRxFilter(&can_pal1_instance, CAN_MSG_ID_STD, RX_MAILBOX_CAN0, 0);
    CAN_InstallEventCallback(&can_pal1_instance, &canCallBackFun, (void *)1);
    CAN_Receive(&can_pal1_instance, RX_MAILBOX_CAN0, &rx_can0_msg);

    CAN_Init(&can_pal2_instance, &can_pal2_Config0);
    CAN_ConfigRxBuff(&can_pal2_instance, RX_MAILBOX_CAN1, &Rx_buffCfg, RX_Filter);
    CAN_ConfigTxBuff(&can_pal2_instance, TX_MAILBOX_CAN1, &Tx_buffCfg);
    CAN_SetRxFilter(&can_pal2_instance, CAN_MSG_ID_STD, RX_MAILBOX_CAN1, 0);
    CAN_InstallEventCallback(&can_pal2_instance, &canCallBackFun, (void *)2);
    CAN_Receive(&can_pal2_instance, RX_MAILBOX_CAN1, &rx_can1_msg);
    CAN_Init(&can_pal3_instance, &can_pal3_Config0);

    CAN_ConfigRxBuff(&can_pal3_instance, RX_MAILBOX_CAN2, &Rx_buffCfg, RX_Filter);
    CAN_ConfigTxBuff(&can_pal3_instance, TX_MAILBOX_CAN2, &Tx_buffCfg);
    CAN_SetRxFilter(&can_pal3_instance, CAN_MSG_ID_STD, RX_MAILBOX_CAN2, 0);
    CAN_InstallEventCallback(&can_pal3_instance, &canCallBackFun, (void *)3);
    CAN_Receive(&can_pal3_instance, RX_MAILBOX_CAN2, &rx_can2_msg);
    return true;
}

void canSend(void) {
    static uint8_t cnt         = 0;
    can_message_t  tx_can0_msg = {
         .cs      = 0,
         .id      = 0x88,
         .data[0] = 0,
         .data[1] = 1,
         .data[2] = 2,
         .data[3] = 3,
         .data[4] = 4,
         .data[5] = 5,
         .data[6] = 6,
         .data[7] = 7,
         .length  = 8,
    };
    tx_can0_msg.data[0] = cnt++;
    CAN_Send(&can_pal1_instance, TX_MAILBOX_CAN0, &tx_can0_msg);
}

void canTask(void) {
    uint16_t can_msgid;
    uint8_t  canrxdata[8];
    uint8_t  can_dlc;
    if (TKS_500MSF) {
        //canSend();  // 发送CAN数据
    }
    if (IRQ_CAN0_RX) {
        IRQ_CAN0_RX = 0;
        can_msgid   = rx_can0_msg.id;
        can_dlc     = rx_can0_msg.length;
        memcpy(canrxdata, rx_can0_msg.data, sizeof(rx_can0_msg.data));
        u1_printf("CAN0接收到数据,ID:%x,数据长度:%d,数据:\r\n", can_msgid, can_dlc);
    }
    if (IRQ_CAN1_RX) {
        IRQ_CAN1_RX = 0;
        can_msgid   = rx_can0_msg.id;
        can_dlc     = rx_can0_msg.length;
        memcpy(canrxdata, rx_can0_msg.data, sizeof(rx_can0_msg.data));
        u1_printf("CAN1接收到数据,ID:%x,数据长度:%d,数据:\r\n", can_msgid, can_dlc);
    }
    if (IRQ_CAN2_RX) {
        IRQ_CAN2_RX = 0;
        can_msgid   = rx_can0_msg.id;
        can_dlc     = rx_can0_msg.length;
        memcpy(canrxdata, rx_can0_msg.data, sizeof(rx_can0_msg.data));
        u1_printf("CAN2接收到数据,ID:%x,数据长度:%d,数据:\r\n", can_msgid, can_dlc);
    }
}
